package com.platform.modules.alg.alglib.poj1019;

import static java.lang.Math.log10;
import static java.lang.Math.pow;

public class Poj1019 {

    public final int maxn = 40000;
    // a[i] 为第 i 组的长度，sum[i] 为前i(包括i)组的总长度
    private long a[] = new long[maxn];
    private long sum[] = new long[maxn];

    public String output = "";

    public String cal(String input) {
        int i;
        sum[0] = a[0] = 0;
        for (i = 1; i < maxn; i++) {
            a[i] = a[i - 1] + (int) log10(i) + 1;
            sum[i] = sum[i - 1] + a[i];
        }
        int t, n;
        String[] line = input.split("\n");
        t = Integer.parseInt(line[0]);
        int num = 1;
        while (t-- > 0) {
            n = Integer.parseInt(line[num]);
            num++;
            i = 0;
            while (sum[i] < n) i++;  // 确定 n 在第 i 组
            long pos = n - sum[i - 1];  //确定 n 在第 i 组的第 pos 个位置
            int len = 0, k = 0;
            while (len < pos) {
                k++;
                len += (int) log10(k) + 1;
            }
            output += k / (int) pow(10.0, len - pos) % 10 + "\n";
        }
        return output;
    }
}
